package com.it.util;

import com.it.pojo.Equation;


import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class EquationFactory {
    private static Random random = new Random();

    public static Equation createAddEquation(){

        return createAddEquation(PropertiesUtil.getMin(),PropertiesUtil.getMax(),PropertiesUtil.getResultMax(),PropertiesUtil.getResultMin());
    }

    public static Equation createSubEquation(){

        return createSubEquation(PropertiesUtil.getMin(),PropertiesUtil.getMax(),PropertiesUtil.getResultMax(),PropertiesUtil.getResultMin());
    }

    public static Equation createEquationByRandom(){

        return createEquationByRandom(PropertiesUtil.getMin(),PropertiesUtil.getMax(),PropertiesUtil.getResultMax(),PropertiesUtil.getResultMin());
    }


    public static Equation createAddEquation(int min, int max, int resultMax, int resultMin) {

        while (true) {
            Equation addEquation = new Equation();

            List<Integer> lefts = new ArrayList<Integer>();
            List<Integer> rights = new ArrayList<Integer>();
            int minTemp = min;
            for (int i = 0; i <= max - min; i++) {
                lefts.add(minTemp++);
            }
            int left = lefts.get(random.nextInt(lefts.size()));

            int right;

            int rightMin = (resultMin - left) < min ? min : (resultMin - left);
            int rightMax = (resultMax - left) < max ? (resultMax - left) : max;


            minTemp = rightMin;
            for (int i = 0; i <= rightMax - rightMin; i++) {
                rights.add(minTemp++);

            }
            if (rights.size() == 0) {
                continue;
            }
            right = rights.get(random.nextInt(rights.size()));


            int result = left + right;

            addEquation.setLeft(left);
            addEquation.setRight(right);
            addEquation.setResult(result);
            addEquation.setOperator("+");
            return addEquation;

        }
    }

    public static Equation createSubEquation(int min, int max, int resultMax, int resultMin) {

        while (true) {

            Equation equation = new Equation();


            List<Integer> lefts = new ArrayList<Integer>();
            List<Integer> rights = new ArrayList<Integer>();
            int minTemp = min;
            for (int i = 0; i <= max - min; i++) {
                lefts.add(minTemp++);
            }
            int left = lefts.get(random.nextInt(lefts.size()));

            int right;

            int rightMin = (left - resultMax) < min ? min : (left - resultMax);
            int rightMax = (left - resultMin) < max ? (left - resultMin) : max;


            minTemp = rightMin;
            for (int i = 0; i <= rightMax - rightMin; i++) {
                rights.add(minTemp++);

            }
            if (rights.size() == 0) {
                continue;
            }
            right = rights.get(random.nextInt(rights.size()));


            int result = left - right;

            equation.setLeft(left);
            equation.setRight(right);
            equation.setResult(result);
            equation.setOperator("-");
            return equation;

        }
    }


    public static Equation createEquationByRandom(int min, int max, int resultMax, int resultMin){
        int chance=random.nextInt(2);
        if(chance==1){
            return createAddEquation(min, max, resultMax, resultMin);
        }else
        {
            return createSubEquation(min, max, resultMax, resultMin);
        }
    }
}




